package com.quanyan.place.wrapper.impl;

import com.google.gson.Gson;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.utils.ObjectUtil;
import com.quanyan.mq.client.reqeust.MqRequestMsg;
import com.quanyan.place.component.MqPlaceProducerClientImpl;
import com.quanyan.place.constants.PlaceConstants;
import com.quanyan.place.entity.PlaceMessage;
import com.quanyan.place.entity.apiresp.PlaceOrderVo;
import com.quanyan.place.entity.vo.MqPlaceGroupId;
import com.quanyan.place.entity.vo.TbBizPlaceVo;
import com.quanyan.place.wrapper.MqPlaceConsumerWrapper;
import com.quanyan.place.wrapper.MqPlaceProductWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Kingson.chan on 2016/8/16 0016.
 */

@Service("mqPlaceProductWrapperImpl")
public class MqPlaceProductWrapperImpl implements MqPlaceProductWrapper {
    private static final Logger log = LoggerFactory.getLogger(MqPlaceProductWrapperImpl.class);

    @Autowired
    MqPlaceProducerClientImpl mqPlaceProducerClient;


    @Override
    public void updatePlaceGroupIdAsyncSend(MqPlaceGroupId mqPlaceGroupId) {
        MqRequestMsg mqRequestMsg = new MqRequestMsg();
        mqRequestMsg.setTopic(PlaceConstants.PLACE_TOPIC);
        mqRequestMsg.setTag(PlaceConstants.PLACE_TAG);
        mqRequestMsg.setMsgId(String.valueOf(mqPlaceGroupId.getId())); //场馆id
        mqRequestMsg.setFlag(MqPlaceConsumerWrapper.MQ_PLACE_GROUP_TYPE);
        mqRequestMsg.setBody(ObjectUtil.toByteArray(mqPlaceGroupId));
        log.info("SEND TO MQ BODY ==> DATA : {}", new Gson().toJson(mqPlaceGroupId).toString());
        mqPlaceProducerClient.asyncSend(mqRequestMsg);
    }

    @Override
    public void pushMessageAsyncSend(PlaceMessage placeMessage) {
        MqRequestMsg mqRequestMsg = new MqRequestMsg();
        mqRequestMsg.setTopic(PlaceConstants.PLACE_TOPIC);
        mqRequestMsg.setTag(PlaceConstants.PLACE_TAG);
        mqRequestMsg.setMsgId(String.valueOf(placeMessage.getOrderNo())); //场馆id
        mqRequestMsg.setFlag(MqPlaceConsumerWrapper.MQ_PLACE_PUSH_MESSAGE_TYPE);
        mqRequestMsg.setBody(ObjectUtil.toByteArray(placeMessage));
        log.info("SEND TO MQ BODY ==> DATA : {}", new Gson().toJson(placeMessage).toString());
        mqPlaceProducerClient.asyncSend(mqRequestMsg);

    }

    @Override
    public void addPlaceIndexAsyncSend(TbBizPlaceVo tbBizPlaceVo) {
        MqRequestMsg mqRequestMsg = new MqRequestMsg();
        mqRequestMsg.setTopic(PlaceConstants.PLACE_TOPIC);
        mqRequestMsg.setTag(PlaceConstants.PLACE_TAG);
        mqRequestMsg.setMsgId(String.valueOf(tbBizPlaceVo.getTbBizPlace().getId())); //场馆id
        mqRequestMsg.setFlag(MqPlaceConsumerWrapper.MQ_PLACE_UPDATE_INDEX_TYPE);
        mqRequestMsg.setBody(ObjectUtil.toByteArray(tbBizPlaceVo));
        log.info("SEND TO MQ BODY ==> DATA : {}", new Gson().toJson(tbBizPlaceVo).toString());
        mqPlaceProducerClient.asyncSend(mqRequestMsg);
    }

    @Override
    public void sendOrderInfoToStadium(PlaceOrderVo placeOrderVo) {
        MqRequestMsg mqRequestMsg = new MqRequestMsg();
        mqRequestMsg.setTopic(Constants.PLACE_ORDER_TOPIC);
        mqRequestMsg.setTag(Constants.PLACE_ORDER_TAG);
        mqRequestMsg.setMsgId(placeOrderVo.getPlaceOrderId()); //订单号
        mqRequestMsg.setFlag(placeOrderVo.getType());
        mqRequestMsg.setBody(ObjectUtil.toByteArray(placeOrderVo));
        log.info("SEND TO MQ BODY ==> DATA : {}", new Gson().toJson(placeOrderVo).toString());
        mqPlaceProducerClient.asyncSend(mqRequestMsg);
    }
}
